package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api("套餐管理相关接口")
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealServiceImpl;

    /*
     * 新增套餐
     * */
    @PostMapping
    @ApiOperation("添加套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("save setmeal:{}", setmealDTO);
        setmealServiceImpl.saveWithDish(setmealDTO);
        return Result.success();
    }

    /*
     * 分页查询套餐
     * */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询套餐{}", setmealPageQueryDTO);
        PageResult result = setmealServiceImpl.pageQuery(setmealPageQueryDTO);
        return Result.success(result);
    }

    /*
    * 回显套餐
    */

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealServiceImpl.getById(id);
        return Result.success(setmealVO);
    }
    /*
    * 修改套餐
    * */
    @PutMapping()
    @ApiOperation("修改套餐数据")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("update setmealDTO:{}", setmealDTO);
        setmealServiceImpl.updateSetmeal(setmealDTO);
        return Result.success();
    }


    /*
    * 删除套餐
    * */
    @DeleteMapping()
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam("ids") List<Long> ids){
        log.info("delete setmeal");
        setmealServiceImpl.deleteSetmealById(ids);
        return Result.success();
    }
    /*
    * 修改套餐状态信息
    * */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long id)
    {
        log.info("update status:{},id:{}", status,id);
        setmealServiceImpl.startOrStop(id,status);
        return null;
    }
}